package com.polaris.live.adapter.login

import android.Manifest
import android.view.View
import com.blankj.utilcode.util.PermissionUtils
import com.bumptech.glide.Glide
import com.luck.picture.lib.basic.PictureSelector
import com.polaris.live.common.adapter.BaseBindingAdapter
import com.polaris.live.common.adapter.VBViewHolder
import com.polaris.live.common.ext.dp
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.ItemAccountAppealBinding
import com.polaris.live.translation.R.string
import com.polaris.live.utils.image.forResultList
import com.polaris.live.utils.image.loadImage
import com.polaris.live.utils.image.openGalleryDefault
import com.polaris.live.utils.image.transformRounded

/**
 * AppealAdapter
 *
 * @author Created by lucas on 2023/11/20 10:26
 * @since 1.0.0
 */
class AppealAdapter : BaseBindingAdapter<ItemAccountAppealBinding, String>() {

    override fun convert(holder: VBViewHolder<ItemAccountAppealBinding>, item: String) {
        holder.vb.apply {
            if (holder.bindingAdapterPosition == itemCount - 1) {
                if (item.isNotEmpty()) {
                    ivAppealCover.visibility = View.GONE
                    ivAppeal.visibility = View.VISIBLE
                    ivDelete.visibility = View.VISIBLE

                    Glide.with(context)
                        .loadImage(item)
                        .transformRounded(8.dp)
                        .into(ivAppeal)
                } else {
                    ivAppealCover.visibility = View.VISIBLE
                    ivAppeal.visibility = View.GONE
                    ivDelete.visibility = View.GONE

                    ivAppealCover.setOnSingleClickListener {
                        PermissionUtils.permissionGroup(
                            *arrayOf(
                                Manifest.permission.CAMERA,
                                Manifest.permission.READ_EXTERNAL_STORAGE,
                                Manifest.permission.WRITE_EXTERNAL_STORAGE
                            )
                        ).callback(object : PermissionUtils.FullCallback {
                            override fun onGranted(granted: List<String>) {
                                PictureSelector.create(holder.itemView.context)
                                    .openGalleryDefault(3 - data.filter { it.isNotEmpty() }.size)
                                    .forResultList {
                                        val pathList = mutableListOf<String>()
                                        pathList.addAll(data.filter { it.isNotEmpty() })
                                        pathList.addAll(it)
                                        if (pathList.isNotEmpty()) {
                                            if (pathList.size < 3) {
                                                pathList.add("")
                                            }
                                            setList(pathList)
                                        }
                                    }
                            }

                            override fun onDenied(
                                deniedForever: List<String>,
                                denied: List<String>
                            ) {
                                ToastGravity.showGravity(string.permission_toast)
                            }
                        }).request()
                    }
                }
            } else {
                ivAppeal.visibility = View.VISIBLE
                ivDelete.visibility = View.VISIBLE
                ivAppealCover.visibility = View.GONE

                Glide.with(context)
                    .loadImage(item)
                    .transformRounded(8.dp)
                    .into(ivAppeal)
            }

            ivDelete.setOnSingleClickListener {
                remove(item)
                if (!data.contains("")) {
                    addData("")
                }
            }
        }
    }
}